//
//  ListDownViewController.m
//  UI_xiangmu
//
//  Created by dllo on 15/4/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ListDownViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "ClassifyOneModel.h"
#import "ClassifyotherCell.h"
#import "detailCamereViewController.h"
@interface ListDownViewController ()

@property(nonatomic,retain)NSMutableArray *modelArr;

@property(nonatomic,retain)UIView *myview;

@property(nonatomic,assign)NSInteger state;

@end

@implementation ListDownViewController

-(void)data
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=listByType&id=%@&page=1&limit=1000",self.canshu];
    [manager GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.modelArr = [NSMutableArray array];
        NSMutableArray *arr = [responseObject objectForKey:@"list"];
        for (NSMutableDictionary *dic in arr) {
            ClassifyOneModel *model = [[ClassifyOneModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:model];
            [model release];
            NSLog(@"%@",model);
        }
        UILabel *zhulabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
        [self.myview addSubview:zhulabel];
        [zhulabel release];
        NSMutableDictionary *dic = [responseObject objectForKey:@"info"];
        zhulabel.text = [dic objectForKey:@"intro"];
        zhulabel.tag = 400;
        UIButton *bu = [UIButton buttonWithType:UIButtonTypeSystem];
        bu.frame = CGRectMake(340, 0, 30, 30);
        [bu setBackgroundImage:[UIImage imageNamed:@"xiangxia.png"] forState:UIControlStateNormal];
        [self.myview addSubview:bu];
        [bu addTarget:self action:@selector(buclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.myTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"解析失败");
    }];
    
    
}
-(void)setview{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 34, 375, 667-34) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [self.myTableView release];
    self.myTableView.rowHeight = 120;
}

-(void)buclick:(UIButton *)santer
{   if(self.state == 0){
    UILabel *label =(UILabel *) [self.myview viewWithTag:400];
      NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    CGRect rect = [label.text boundingRectWithSize:CGSizeMake(300,10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.myview.frame = CGRectMake(0, 64, 375, 30+rect.size.height);
    label.frame = CGRectMake(0, 0, 300, rect.size.height);
    self.myTableView.frame = CGRectMake(0, 34+rect.size.height, 375, 667-34-rect.size.height);
    label.numberOfLines = 0;
    [santer setBackgroundImage:[UIImage imageNamed:@"xiangshang.png"] forState:UIControlStateNormal];
    [label sizeToFit];
    santer.frame = CGRectMake(340, rect.size.height, 30, 30);
    self.state = 1;
}else{
    UILabel *label =(UILabel *) [self.myview viewWithTag:400];
    self.myview.frame = CGRectMake(0, 64, 375, 30);
    label.frame = CGRectMake(0, 0, 300, 20);
    label.numberOfLines = 1;
    santer.frame = CGRectMake(340, 0, 30, 30);
    [santer setBackgroundImage:[UIImage imageNamed:@"xiangxia.png"] forState:UIControlStateNormal];
    self.state = 0;
    self.myTableView.frame = CGRectMake(0,34, 375,667-34);
}
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self data];
    [self setview];
    self.myview = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 375, 30)];
    self.myview.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.myview];
    [_myview release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    ClassifyotherCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[ClassifyotherCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    cell.namelabel.text = [self.modelArr[indexPath.row] name];
    
    NSString *str1 = [NSString stringWithFormat:@"%@分",[self.modelArr[indexPath.row]score]];
    cell.scorelabel.text = str1;
    ClassifyOneModel *model = self.modelArr[indexPath.row];
    NSLog(@"%@",model);
    NSURL *url = [NSURL URLWithString:[self.modelArr[indexPath.row] cover]];
    [cell.myimage sd_setImageWithURL:url];
    NSString *str2 = [self.modelArr[indexPath.row] is_over];
    NSString *str3 = [self.modelArr[indexPath.row] cur_num];
    if ([str2 isEqualToString:@"0"]) {
        NSString *str = [NSString stringWithFormat:@"更至%@集",str3];
        cell.jishulabel.text = str;
    }else{
        NSString *str = [NSString stringWithFormat:@"全%@集",str3];
        cell.jishulabel.text = str;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailCamereViewController *de = [[detailCamereViewController alloc] init];
    de.canshu = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=getInfo&video_id=%@&uid=(null)",[self.modelArr[indexPath.row] video_id]];
    [self.navigationController pushViewController:de animated:YES];
    [de release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
